home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.4 Applications 1997 August / SGI IRIX 6.4 Applications 1997 August.iso / dist / gateway.idb / usr / WebFace / Source / 20-NetworkServices / routing / gateways-config.frm.z / gateways-config.frm
Encoding:
Text File  |  1997-07-30  |  11.3 KB  |  412 lines

  1. #!/usr/bin/perl5
  2. #
  3. # gateways-config.cgi
  4. #
  5. # Copyright 1988-1996 Silicon Graphics, Inc.
  6. # All rights reserved.
  7. #
  8. # This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  9. # the contents of this file may not be disclosed to third parties, copied or
  10. # duplicated in any form, in whole or in part, without the prior written
  11. # permission of Silicon Graphics, Inc.
  12. #
  13. # RESTRICTED RIGHTS LEGEND:
  14. # Use, duplication or disclosure by the Government is subject to restrictions
  15. # as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  16. # and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  17. # successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  18. # rights reserved under the Copyright Laws of the United States.
  19. #
  20. # $Id: gateways-config.frm,v 1.36 1997/06/24 22:20:51 shotes Exp $
  21.  
  22. require "/usr/OnRamp/lib/OnRamp.pm";
  23. require "/usr/OnRamp/lib/java.pm";
  24.  
  25. $conf = "/etc/gateways";
  26. $dummy = "/etc/gateways.dummy";
  27. $myname = "gateways-config.cgi";
  28. $title = "Gateway Configuration";
  29.  
  30. $js_main =
  31. "type = \"address\";
  32. $js_account_main
  33. $js_error_box
  34. $js_ip
  35. function testAdd(form) {
  36.     if(form.destination.value == \"\") {
  37.         errorBox (form.destination, \"To add a new gateway, first enter \\na \"
  38.             + \"destination IP address.\");
  39.         return (false);
  40.     }
  41.     if (!testIPaddress(form.destination.value, false)) {
  42.         errorBox (form.destination, \"The destination address \" + 
  43.             form.destination.value + \"\\nis invalid.\");
  44.         return (false);
  45.     }
  46.     return (true);
  47. }";
  48.  
  49. $js_add =
  50. "$js_standard
  51. $js_error_box
  52. $js_ip
  53. function checkForm(form)  {
  54.     if (form.route.value == \"\") { 
  55.         errorBox(form.route, \"Route interface required.\");
  56.     }
  57.     if (!testIPaddress(form.route.value, false)) {
  58.         errorBox(form.route, \"Invalid route interface address.\");
  59.         return (false);
  60.     }
  61.     if (!testInt(form.metric)) return (false);
  62.     return (true);
  63. }
  64. function testInt(Ctrl) {
  65.     if (Ctrl.value == \"\") { 
  66.         errorBox (Ctrl, \"The number of hops is required.\"); 
  67.         return (false); 
  68.     }
  69.     start = 0;  
  70.     while (Ctrl.value.charAt(start) == ' ') { start++; }
  71.     end = Ctrl.value.length-1; 
  72.     while (Ctrl.value.charAt(end) == ' ') { end--; }
  73.     for (n = start; n <= end; n++) {
  74.         digit = Ctrl.value.charAt(n); 
  75.         if(! checkInt_c(digit, 10)) { 
  76.             errorBox (Ctrl, \"The number of hops must be \\na valid integer.\"); 
  77.             return (false); 
  78.         }
  79.     }
  80.     str = Ctrl.value.substring(start,end+1); num = parseInt(str);
  81.     if (num > 16) { 
  82.         errorBox (Ctrl, \"The number of hops must be \\nless than 16.\"); 
  83.         return (false); 
  84.     }
  85.     return (true);
  86. }";
  87.  
  88. $js_edit =
  89. "$js_standard
  90. $js_error_box
  91. $js_ip
  92. function checkForm(form) {
  93.     if (form.route.value == \"\") { 
  94.         errorBox(form.route, \"Route interface required.\");
  95.     }
  96.     if (!testIPaddress(form.route.value, false)) {
  97.         errorBox(form.route, \"Invalid route interface address.\");
  98.         return (false);
  99.     }
  100.     if (!testInt(form.metric)) return (false);
  101.     return (true);
  102. }
  103. function testInt(Ctrl) {
  104.     if (Ctrl.value == \"\") { 
  105.         errorBox (Ctrl, \"The number of hops is required.\"); 
  106.         return (false); 
  107.     }
  108.     start = 0;  
  109.     while (Ctrl.value.charAt(start) == ' ') { start++; }
  110.     end = Ctrl.value.length-1; 
  111.     while (Ctrl.value.charAt(end) == ' ') { end--; }
  112.     for (n = start; n <= end; n++) {
  113.         digit = Ctrl.value.charAt(n); 
  114.         if(! checkInt_c(digit, 10)) { 
  115.             errorBox (Ctrl, \"The number of hops must be \\na valid integer.\"); 
  116.             return (false); 
  117.         }
  118.     }
  119.     str = Ctrl.value.substring(start,end+1); num = parseInt(str);
  120.     if (num > 16) { 
  121.         errorBox (Ctrl, \"The number of hops must be \\nless than 16.\"); 
  122.         return (false); 
  123.     }
  124.     return (true);
  125. }";
  126.  
  127.  
  128. print "Content-type: text/html\n\n";
  129.  
  130. &get_fields;
  131. &getGateways;
  132.  
  133. $delete = 0;
  134.  
  135. if (%fld) {
  136.     $help = $document_root . $ENV{"SCRIPT_NAME"};
  137.     $help =~ s/cgi$/hlp/;
  138.     exec $help if ($fld{'help'} eq "Help");
  139.  
  140.     $fld{'chosen'} =~ /([\w.]+)/;
  141.     $fld{'chosen'} = $1;
  142.     if ($fld{"add"}) { 
  143.         &validAdd;
  144.         $val{'metric'} = 1;
  145.         &getAdd;
  146.         exit 0; 
  147.     }
  148.     elsif ($fld{"doAdd"}) {
  149.         &valid(0);
  150.         &doAdd;
  151.         $fld{'destination'} = "";
  152.         &getGateways;
  153.     &routed_hup;
  154.     }
  155.     elsif ($fld{"doEdit"}) {
  156.         &valid(1);
  157.         &doEdit;
  158.         &getGateways;
  159.     &routed_hup;
  160.     }
  161.     elsif ($fld{"edit"}) {
  162.         &error(2,"To edit an existing gateway, first choose one from the list,
  163.             then click the edit button.") if !$fld{'chosen'};
  164.         &getGateways($fld{'chosen'});
  165.         &putEdit;
  166.         &getEdit;
  167.         exit 0; 
  168.     }
  169.     elsif ($fld{"delete"}) { 
  170.         &error(2,"To delete an existing gateway, first choose one from the 
  171.             list, then click the delete button.") if !$fld{'chosen'};
  172.         $delete = 1;
  173.         $message = qq|Click "Ok" to save changes.|; 
  174.     }
  175.     elsif ($fld{"doit"}) { 
  176.         if ($erd ne $fld{'erd'}) { &changeStatus; }
  177.         if ($fld{'delGateway'}) { &doDelete; };
  178.         &getGateways;
  179.     &routed_hup;
  180.     }
  181.     else {
  182.         $message = "Use buttons to submit form.";
  183.     }
  184.  
  185.     if (!$message) { $message = "No change made."; }
  186.     %val = %fld; 
  187.  
  188. &generic;
  189.  
  190. sub routed_hup {
  191.     system("/sbin/killall", "routed");
  192.     
  193.     open(IN,"< /etc/config/routed.options");
  194.     while(<IN>) {
  195.         chop($_);
  196.         $configFile .= " $_";
  197.     }
  198.     close(IN);
  199.  
  200.     system("/usr/etc/routed $configFile > /dev/null 2>&1");
  201. }
  202.  
  203. sub valid {
  204.     &error($_[0],"Invalid route address.") if !$fld{'route'} || &check_ipaddr($fld{'route'});
  205.     &error($_[0],"Number of hops must be an integer.") if $fld{'metric'} =~ /[^0-9-]/;
  206.     &error($_[0],"Number of hops must be greater than zero.") if $fld{'metric'} < 1;
  207.     &error($_[0],"Number of hops must be no greater than 16.") if $fld{'metric'} > 16;    
  208.  
  209. sub validAdd {
  210.     &error(2,"You must provide a gateway address.") if !$fld{'destination'};
  211.     &error(2,"Invalid gateway address.") if &check_ipaddr($fld{'destination'});   
  212. }
  213.  
  214. sub error {
  215.     &error_block($_[1]);
  216.     %val = %fld;
  217.     if ($_[0] == 0) { &getAdd; }
  218.     elsif ($_[0] == 1) { &getEdit; }
  219.     else { &generic; }
  220.     exit 0;
  221. }
  222.  
  223. sub changeStatus {
  224.     if ($fld{'erd'} eq 'Yes') { 
  225.     system("/etc/chkconfig", "routed", "on");
  226.     system("/etc/killall", "routed");
  227.     open(IN,"< /etc/config/routed.options");
  228.     while(<IN>) {
  229.         chop($_);
  230.         $configFile .= " $_";
  231.     }
  232.     close(IN);
  233.     system("/usr/etc/routed $configFile > /dev/null 2>&1");
  234.     $message = "Dynamic routing enabled.";
  235.     }
  236.     else {
  237.     system("/etc/chkconfig", "routed", "off");
  238.     system("/etc/killall", "routed");
  239.         $message = "Dynamic routing disabled.";
  240.     }
  241. }
  242.    
  243. sub doDelete {
  244.     open(IN,"< $conf");
  245.     open(OUT,"> $dummy");
  246.     while(<IN>) {
  247.     @items = split(/\s+/);
  248.     if ($items[1] eq $fld{'delGateway'}) { print OUT "# $_"; }
  249.     else { print OUT $_; }
  250.     }
  251.     close(IN);
  252.     close(OUT);
  253.     rename($dummy,$conf);
  254.  
  255.     $message = "Gateway deleted.";
  256. }
  257.  
  258. sub doAdd {
  259.     open(OUT,">> $conf");
  260.     print OUT "$fld{'type'} $fld{'destination'} gateway $fld{'route'} ";
  261.     print OUT "metric $fld{'metric'} $fld{'mode'}\n";
  262.     close(OUT);
  263.  
  264.     $message = "Gateway added.";
  265. }
  266.  
  267. sub putEdit {
  268.     $val{'type'} = $type;
  269.     $val{'mode'} = $mode;
  270.     $val{'route'} = $rout;
  271.     $val{'metric'} = $metr;
  272. }
  273.  
  274. sub doEdit {
  275.     open(IN,"< $conf");
  276.     open(OUT,"> $dummy");
  277.     while(<IN>) {
  278.     @items = split(/\s+/);
  279.     if ($items[1] eq $fld{'chosen'}) { print OUT "# $_"; }
  280.     else { print OUT $_; }
  281.     }
  282.     print OUT "$fld{'type'} $fld{'chosen'} gateway $fld{'route'} ";
  283.     print OUT "metric $fld{'metric'} $fld{'mode'}\n";
  284.     close(IN);
  285.     close(OUT);
  286.     rename($dummy,$conf);
  287.  
  288.     $message = "Gateway edited.";
  289. }
  290.     
  291. sub getEdit {
  292.     &js_title_block($title,$js_edit);
  293.     &header_block("Edit Gateway");
  294.  
  295.     print "<form name=StandardForm action=$myname method=post onSubmit=\"return runSubmit()\">\n";
  296.  
  297.     print "<input type=hidden name=chosen value=$fld{'chosen'}>";
  298.  
  299.     print "<center><table width=450>";
  300.  
  301.     print "<tr><th align=left>IP address of gateway destination:</th><td>
  302.         <tt>$fld{'chosen'}</tt></td></tr>";
  303.     print "<tr><th align=left>IP address of route interface:</th><td>",
  304.         &text('route',$val{'route'},16),"</td></tr>";
  305.     print "<tr><th align=left>Type of network gateway:</th><td>",
  306.         &select('type',$val{'type'},'net','host'),"</td></tr>";
  307.     print "<tr><th align=left>Mode of network gateway:</th><td>",
  308.         &select('mode',$val{'mode'},'active','passive'),"</td></tr>";
  309.     print "<tr><th align=left>Number of hops from route interface to 
  310.             destination:</th><td>",
  311.         &text('metric',$val{'metric'},5),"</td></tr>";
  312.  
  313.     print "</table></center>";
  314.  
  315.     print &js_buttons('doEdit','Ok','onClick="markOK()"','onClick="markOther()"');
  316.     
  317.     print '</form>';
  318. }
  319.  
  320. sub getAdd {
  321.     &js_title_block($title,$js_add);
  322.     &header_block("Add New Gateway");
  323.  
  324.     print "<form name=StandardForm action=$myname method=post onSubmit=\"return runSubmit()\">\n";
  325.  
  326.     print "<input type=hidden name=destination value=$fld{'destination'}>";
  327.  
  328.     print "<center><table width=450>";
  329.  
  330.     print "<tr><th align=left>IP address of gateway destination:</th><td>
  331.         <tt>$fld{'destination'}</tt></td></tr>";
  332.     print "<tr><th align=left>IP address of route interface:</th><td>",
  333.         &text('route',$val{'route'},16),"</td></tr>";
  334.     print "<tr><th align=left>Type of network gateway:</th><td>",
  335.         &select('type',$val{'type'},'net','host'),"</td></tr>";
  336.     print "<tr><th align=left>Mode of network gateway:</th><td>",
  337.         &select('mode',$val{'mode'},'active','passive'),"</td></tr>";
  338.     print "<tr><th align=left>Number of hops from route interface to 
  339.             destination:</th><td>",
  340.         &text('metric',$val{'metric'},5),"</td></tr>";
  341.  
  342.     print "</table></center>";
  343.  
  344.     print &js_buttons('doAdd','Ok','onClick="markOK()"','onClick="markOther()"');
  345.     
  346.     print '</form>';
  347. }
  348.    
  349. sub generic {
  350.     &js_title_block($title,$js_main);
  351.     &header_block($title);
  352.  
  353.     print "<i>$message</i>";
  354.  
  355.     print "<form name=AccountForm action=$myname method=post onSubmit=\"return runSubmit()\">\n";
  356.  
  357.     print "<center><table width=450>\n";
  358.  
  359.     print "<tr><th align=left>Enable dynamic routing:</th><th align=left>",
  360.         &radio('erd',$erd,'Yes','No'),"</th></tr>\n";
  361.  
  362.     print "<tr><th align=left>";
  363.     print qq|<input type=submit name=add value="Add New Gateway" onClick="markAdd()"></th>|;
  364.     print "\n<td><input size=19 type=text name=destination 
  365.         value=$val{'destination'}>";
  366.     print "</td></tr>\n";
  367.  
  368.     if ($delete) { 
  369.     undef @locList;
  370.     foreach $arg (@gateways) {
  371.         if ($arg ne $fld{'chosen'}) { 
  372.         push(@locList,$arg); }
  373.     }
  374.     print "<input type=hidden name=delGateway value=$fld{'chosen'}>\n"; 
  375.     } else { @locList = @gateways; }
  376.  
  377.     print "<tr><th align=left>";
  378.     print qq|<input type=submit name=edit value="Edit Selected Gateway" onClick="markEdit()"></th>|;
  379.     print "\n<td rowspan=2>", &choice_list(*locList,"chosen",20), "</td></tr>\n";
  380.  
  381.     print "<tr><th align=left>";
  382.     print qq|<input type=submit name=delete value="Delete Selected Gateway" onClick="markDelete()"></th></tr>|;
  383.  
  384.     print "\n</table></center>\n";
  385.  
  386.     print &js_buttons('doit','Ok','onClick="markOther()"','onClick="markOther()"');
  387.     
  388.     print '</form>';
  389. }
  390.  
  391. sub getGateways {
  392.     undef @gateways;
  393.  
  394.     open(GATE, "< $conf");
  395.     while (<GATE>) {
  396.     @items = split(/\s+/);
  397.     if ($items[0] ne 'net' && $items[0] ne 'host') { next; }
  398.     push(@gateways,$items[1]);
  399.     if ($items[1] eq $_[0]) {
  400.         $type = $items[0];
  401.         $rout = $items[3];
  402.         $metr = $items[5];
  403.         $mode = $items[6];
  404.     }
  405.     }
  406.     close(GATE);
  407.  
  408.     $erd = &get_config("routed");
  409. }
  410.